#include<iostream> 
#include<vector>
using namespace std;

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
		int sum_gas = 0, sum_cost = 0;
		for(int i = 0; i < gas.size(); i++){
			sum_gas += gas[i];
			sum_cost += cost[i];
			gas[i] -= cost[i];
		}
		if(sum_gas < sum_cost)return -1;
		int ans = 0, temp = 0;
		for(int i = 0; i < gas.size(); i++){
			if(!temp){
				if(gas[i] < 0)continue;
				else{
					ans = i; 
					temp += gas[i];
				}
			}
			else{
				if(gas[i] < 0){
					temp += gas[i];
					if(temp < 0){
						temp = 0;
						ans = 0;
					}
				}
				else{
					temp += gas[i];
				}
			}
		} 
		return ans;
    }
};

int main(){
	return 0;
}

//2020.11.18

